package com.zzmemg.test_flutter

import android.util.Log
import io.flutter.plugin.common.BasicMessageChannel
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.StandardMessageCodec

class MessageChannel(flutterEngine: BinaryMessenger) : BasicMessageChannel.MessageHandler<Any> {

    companion object {
        private const val MESSAGE_CHANNEL_NAME = "cn.xqw/message"
        private const val TAG = "MessageChannel"
    }

    private var messageChannel: BasicMessageChannel<Any>
    private var index = 0;


    init {
        messageChannel = BasicMessageChannel(flutterEngine, MESSAGE_CHANNEL_NAME, StandardMessageCodec())
        messageChannel.setMessageHandler(this)
    }

    override fun onMessage(message: Any?, reply: BasicMessageChannel.Reply<Any>) {
        message?.let {
            val params = message as HashMap<String, Any>
            val name = params["name"]
            val age = params["age"]
            Log.i(TAG, "==== get flutter message name:$name, age:$age")
            reply.reply(HashMap<String, Any>().apply {
                put("code", 0)
                put("msg", "success:${index++}")
            })
        }
    }
}